﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace LoomDataEmulator.Core
{
    public class LoomDataServer
    {
        #region Variables
        private string _filePath;
        private IInputProcessor _processInput;
        private IScheduler _scheduler;
        #endregion

        #region Constructor

        public LoomDataServer(IInputProcessor processInput, IScheduler scheduler)
        {
            if(processInput == null)
                throw new NullReferenceException("Process input should not be null!");

            if (scheduler == null)
                throw new NullReferenceException("scheduler should not be null!");

            ProcessInput = processInput;
            Scheduler = scheduler;

        }
        #endregion

        #region Properties

        public IInputProcessor ProcessInput
        {
            get { return _processInput; }
            set { _processInput = value; }
        }

        public IScheduler Scheduler
        {
            get { return _scheduler; }
            set { _scheduler = value; }
        }

        public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        #endregion

        #region Protected Methods

        protected void ProcessDataFiles()
        {
        }

        #endregion



        public void ListenPath(string path)
        {
            this.ValidatePath(path);

            FileSystemWatcher CMIFileWatcher = new FileSystemWatcher();
            CMIFileWatcher.Path = path;
            CMIFileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            CMIFileWatcher.Filter = "*.cmi";
            CMIFileWatcher.Created += new FileSystemEventHandler(_processInput.ProcessInput);
            CMIFileWatcher.EnableRaisingEvents = true;

        }


        protected void ValidatePath(string path)
        {
            if (path == null)
                throw new PathNullException();

            if (!(new DirectoryInfo(path).Exists))
                throw new DirectoryNotFoundException();
        }


    }
}
